package com.gjbao.httpcument.emils;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

public class SendUtils {

    private static  String HOST = "smtphz.qiye.163.com";
    private static  String USERNAME = "administrator@adks.cn";
    private static  String PASSWORD = "AdconCepts@2017";
    private static  String personal = "adks";

    public static void main(String[] args) throws MessagingException, UnsupportedEncodingException {

        JavaMailSenderImpl mailSender = createMailSender("200981451@qq.com", "Qq200981451");
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        // 设置utf-8或GBK编码，否则邮件会有乱码
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        messageHelper.setFrom("200981451@qq.com","测试账户");
    }

    /**
     * 邮件发送器    --  自定义邮箱
     *
     * @return 配置好的工具
     */
    private static JavaMailSenderImpl createMailSender(String Username, String Password) {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setHost(HOST);
        sender.setUsername(Username);
        sender.setPassword(Password);
        sender.setDefaultEncoding("Utf-8");
        Properties p = new Properties();
//        p.setProperty("mail.smtp.timeout", timeout);
        p.setProperty("mail.smtp.auth", "false");
        p.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        sender.setJavaMailProperties(p);
        return sender;
    }
}
